package engine.ui
{
	import engine.data.ModelLocator;
	import engine.logic.AlertLogic;
	import engine.ui.lite.BaseUI;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.utils.Timer;
	
	import newx.com.config.StaticIni;
	import newx.com.control.button.NxButton;
	import newx.com.control.text.NxLabel;
	import newx.com.control.text.NxText;
	import newx.com.control.window.NxWindowEvent;

	/**
	 *	定点自动消失的强制显示的提示框 
	 * @author lw
	 * 
	 */
	public class AlertTimeLoseUI extends BaseUI
	{
		//提示text的背景
		public var tipsTextBg	:	MovieClip;
		//提示text
		public var tipsText	: 	NxText;
		//显示时间label的背景
		public var timeLabelBg	:	MovieClip;
		
		public var alertTips	:	MovieClip;
		//显示时间label
		public var timeLabel	:	NxLabel;
		public var btn			:	NxButton;
		
		//调整地址
		public var url			:	String = "";
		
		private var _model 	: 	ModelLocator;
		
		//计时器
		private var _timer 	: 	Timer;
		
		//显示的最长时间
		private var _maxTime 	: 	int;
		
		private var _tipStr	:	String;
		
		
		private static var _self	: AlertTimeLoseUI;
		
		public static function getInstance():AlertTimeLoseUI
		{
			if(_self == null)
			{
				_self = new AlertTimeLoseUI();
			}
			return _self;
		}
		
		public function AlertTimeLoseUI()
		{
			
		}
		
		override public function Init() : void
		{
			_model = ModelLocator.getInstance();
			btn.label = StaticIni.getSwfLangStr("UIStr_1001360");
			windowBG.closeBtnVisible = false;
			
			_timer = new Timer(1000);
			_timer.addEventListener(TimerEvent.TIMER, onTimer);
		
			url	=	_model.mainIni.getContent("fangchenmi","url");
		}
		
		private function addListener() : void
		{
			windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,		onClose);
			btn.addEventListener(MouseEvent.CLICK, onClick);
		}
		
		private function removeListener() : void
		{
			windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,	onClose);
			btn.removeEventListener(MouseEvent.CLICK, onClick);

		}
		
		private function onClick(evt : MouseEvent):void
		{			
			navigateToURL(new URLRequest(url),"_blank");
			alertTips.stop();
			alertTips.visible = false;
		}
		
		override protected function BeforeShow():void
		{
			addListener();
			alertTips.visible = true;
			alertTips.play();
		}
		
		override protected function AfterHide():void
		{
			removeListener();
			alertTips.stop();
		}
		
		/**
		 *	显示强制提示面板 
		 * @param evt
		 * 
		 */
		public function showTimeLoseUI(strId : String, type : int, msg : String) : void
		{
			tipsText.htmlStr = msg;
			
			//防沉迷
			if(type == AlertLogic.TIPSTYPE_SYSTEM_UNENTHRALL)
			{
				_maxTime = StaticIni.getIniObj("Stop_PlayGame_Time_Out").r;	
			}
			
			if(strId == "17012")
			{
				
				_tipStr	= "F_Gem_13261";
			}else
			{
				_tipStr	= "F_Gem_13241";
			}
			
			_timer.reset();
			_timer.start();
		}
		
		/**
		 *	1秒调用一次 
		 * @param evt
		 * 
		 */
		private function onTimer(evt : TimerEvent) : void
		{
			_maxTime--;
			
			if(_maxTime <= 0)
			{
				_timer.stop();
				hide();
				return;
			}
			
			timeLabel.htmlStr = StaticIni.getSwfLangStrVar(_tipStr, [_maxTime.toString()]);
			
			if(parent == null)
			{
				show();
			}
		}
		
		/**
		 *	 关闭界面
		 */
		private function onClose(evt : NxWindowEvent) : void
		{
			hide();
		}
	}
}